﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_Admin_Admin_QuanLyQuangCao : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        DataDSTableAdapters.QUANGCAOTableAdapter qcAdapter = new DataDSTableAdapters.QUANGCAOTableAdapter();
        DataList_DSQuangCao.DataSource = qcAdapter.GetData();
        if (!IsPostBack)
        {
            DataList_DSQuangCao.DataBind();
            for (int i = 0; i < DataList_DSQuangCao.Items.Count; i++)
                ((Button)(DataList_DSQuangCao.Items[i].FindControl("btnXoa"))).Attributes["OnClick"] = "return confirm('Bạn thật sự muốn xóa?');";
        }

       
        
        
        //qcAdapter.Fill();

    }
   
   
    protected void DataList_DSQuangCao_UpdateCommand(object source, DataListCommandEventArgs e)
    {
       
        int vitri = Convert.ToInt32(e.CommandArgument);
        FileUpload fu = (FileUpload)e.Item.FindControl("FileUpload1");

        int Height = int.Parse(((TextBox)e.Item.FindControl("txtCao")).Text);

        int Width = int.Parse(((TextBox)e.Item.FindControl("txtRong")).Text);
        bool show = ((CheckBox)e.Item.FindControl("ckHienThi")).Checked;
        DataDSTableAdapters.QUANGCAOTableAdapter qcAdapter = new DataDSTableAdapters.QUANGCAOTableAdapter();
        string duongdan = "";
        if (fu.FileName != "")
        {
            duongdan = ConfigurationSettings.AppSettings["FLASH_FOLDER"].ToString() + fu.FileName;
            fu.SaveAs(Server.MapPath(duongdan));

        }
         
        if (duongdan == "")
            duongdan = ((Label)e.Item.FindControl("lbDuongDan")).Text;

        int idDoiTac = int.Parse(((DropDownList)(e.Item.FindControl("ddlDoiTac"))).SelectedValue);
        qcAdapter.QUANGCAO_CapNhatTheoViTri(idDoiTac, duongdan, Height, Width, show, vitri);
        DataList_DSQuangCao.DataBind();
        Response.Write("<script>alert('Cập nhật thành công');location.href='/SMO/ChucNang/Admin/Admin_QuanLyQuangCao.aspx'</script>");
   
        
    }

    protected void DataList_DSQuangCao_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int vitri = Convert.ToInt32(e.CommandArgument);
        DataDSTableAdapters.QUANGCAOTableAdapter qcAdapter = new DataDSTableAdapters.QUANGCAOTableAdapter();
        int? kq = 0; 
        qcAdapter.QUANGCAO_XoaTheoViTri(vitri, ref kq);
        if(kq == 0)
            Response.Write("<script>location.href='/SMO/ChucNang/Admin/Admin_QuanLyQuangCao.aspx'</script>");
        else
            Response.Write("<script>alert('Xoá thất bại!');location.href='/SMO/ChucNang/Admin/Admin_QuanLyQuangCao.aspx'</script>");
    }
    protected void btnThemQC_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_ThemQuangCao.aspx");
    }
    protected void DataList_DSQuangCao_ItemCreated(object sender, DataListItemEventArgs e)
    {
        
        
    }
    protected void DataList_DSQuangCao_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                DataDSTableAdapters.DOITACTableAdapter da = new DataDSTableAdapters.DOITACTableAdapter();
                DataDS.DOITACDataTable tb = da.DOITAC_LayTatCa();

                DropDownList ddlDT = (DropDownList)e.Item.FindControl("ddlDoiTac");
                ddlDT.DataSource = tb;
                ddlDT.DataTextField = "TenDoiTac";
                ddlDT.DataValueField = "Id";
                //ddlDT.DataBind();
                //Label lb = (Label)(e.Item.FindControl("lbIdDoiTac"));
                int idDoiTac = int.Parse(((Label)(e.Item.FindControl("lbIdDoiTac"))).Text);
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if ((int)tb.Rows[i]["Id"] == idDoiTac)
                    {
                        ddlDT.SelectedIndex = i;
                        break;
                    }
                }
                ddlDT.DataBind();

            }
            catch (System.Exception ex)
            {

            }

        }
    }
}
